<?php
// +----------------------------------------------------------------------
// | YBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.yhustc.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yhustc <yhustc@gmail.com>
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * YBlog评论管理模块
 +------------------------------------------------------------------------------
 * @author    yhustc <yhustc@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */


class CommentAction extends BaseAction
{
	/**
     +----------------------------------------------------------
     * 评论列表
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function index()
	{
		
		$where = array();
		switch($_GET["filter"])
		{
			case "0": $where["Comments.comment_approved"] = "0";
				break;
			case "1": $where["Comments.comment_approved"] = "1";
				break;
			default:
				break;
		}
		
		$commentDao = D("CommentView");
		$total = $commentDao->count($where);
		import("ORG.Util.Page");
		$page = new Page($total,20);
		$commentList = $commentDao->where($where)->order("comment_date desc")->limit($page->firstRow.",".$page->listRows)->findAll();
		$this->assign("pageFile","Comment:index");
		$this->assign("filterList",array(""=>"全部评论","1"=>"已审核","0"=>"未审核"));
		$this->assign("selectFilter",$_GET["filter"]);
		$this->assign("commentList",$commentList);
		$this->assign("navigation",$page->show());
		$this->layout();
	}
	
	/**
     +----------------------------------------------------------
     * 编辑评论
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function edit()
	{
		if(!empty($_POST["comment_ID"]))
		{
			//保存修改
			$commentDao = D("Comments");
			$rs = $commentDao->save($_POST);
			
			if($rs)
			{
				$this->assign("jumpUrl",url("index"));
				$this->success("修改评论成功");
			}
			else
				$this->error("未对评论做任何修改");
		}
		else
		{
			$commentDao = D("Comments");
			$comment = $commentDao->find("`comment_ID`='".$_GET["ID"]."'");
			
			$this->assign("pageFile","Comment:edit");
			$this->assign("comment",$comment);
			$this->layout();
		}
	}
	
	/**
     +----------------------------------------------------------
     * 审核评论
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function approve()
	{
		$commentDao = D("Comments");
		$rs = $commentDao->setField("`comment_approved`",$_GET["value"],"`comment_ID`='".$_GET["ID"]."'");
		if($rs)
			$this->success("修改审核状态成功");
		else
			$this->error("没有改变审核状态");
	}
	
	/**
     +----------------------------------------------------------
     * 删除指定评论
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function del()
	{
		$rs = 0;
		$commentDao = D("Comments");
		if(!empty($_GET["ID"]))
			$rs = $commentDao->delete("`comment_ID`='".$_GET["ID"]."'");
		else
			$rs = $commentDao->delete("`comment_ID` IN (".implode(",",$_POST["comment_IDs"]).")");
		
		if($rs)
		{
			//由于可能一次性删除多个日志中的多条评论,所以直接使用SQL重建评论数
			$commentDao->execute("UPDATE `".C("DB_PREFIX")."posts` p SET comment_count = ( SELECT COUNT( `comment_ID` ) FROM `".C("DB_PREFIX")."comments` c WHERE c.`comment_post_ID` = p.`ID` AND p.`post_type`='post')");
			$this->success("删除评论成功");
		}
		else
			$this->error("删除评论失败");
	}
}
?>